package com.daigoudao.web.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.daigoudao.util.Constants;
import com.daigoudao.web.common.BasePageAction;
import com.daigoudao.web.model.AnnounceModel;
import com.daigoudao.web.model.BillModel;
import com.daigoudao.web.model.CanBuyModel;
import com.daigoudao.web.model.InviteModel;
import com.daigoudao.web.model.ShortMessage;
import com.daigoudao.web.model.TransInfoModel;
import com.daigoudao.web.model.UserModel;
import com.daigoudao.web.service.IAnnounceService;
import com.daigoudao.web.service.ICanBuyService;
import com.daigoudao.web.service.IInviteService;
import com.daigoudao.web.service.IShortMessageService;
import com.daigoudao.web.service.ITransInfoService;

public class AgentInfoAction extends BasePageAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger logger = LoggerFactory
			.getLogger(AgentInfoAction.class);
	private ITransInfoService transInfoService;
	private ICanBuyService canBuyService;
	private IInviteService inviteService;
	private IShortMessageService shortMessageService;

	private IAnnounceService announceService;

	private AnnounceModel am;

	private List<TransInfoModel> orders;
	private List<CanBuyModel> canBuyList;
	private List<BillModel> billList;
	private List<ShortMessage> messageList;
	private List<InviteModel> inviteList;
	private String name;
	private int status;
	private UserModel userModel;

	private int canBuyNum;

	private int deliveryNum;

	private int inviteNum;

	private int evaluateNum;

	private int messageNum;

	private int state = -1;

	private int userEvaluation;

	private int[] smids;

	private int receiverId;

	private String content;

	private int userType;

	private ShortMessage shortMessage;

	private int receiver;

	private int replyId;

	private int smid;
	
	

	public void setUserType(int userType) {
		this.userType = userType;
	}

	public int getUserType() {
		return userType;
	}

	public ShortMessage getShortMessage() {
		return shortMessage;
	}

	public int[] getSmids() {
		return smids;
	}

	public int getMessageNum() {
		return messageNum;
	}

	public int getCanBuyNum() {
		return canBuyNum;
	}

	public int getDeliveryNum() {
		return deliveryNum;
	}

	public int getInviteNum() {
		return inviteNum;
	}

	public int getEvaluateNum() {
		return evaluateNum;
	}

	public int getReceiverId() {
		return receiverId;
	}

	public void setReceiverId(int receiverId) {
		this.receiverId = receiverId;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setSmids(int[] smids) {
		this.smids = smids;
	}

	public void setMessageList(List<ShortMessage> messageList) {
		this.messageList = messageList;
	}

	public List<ShortMessage> getMessageList() {
		return messageList;
	}

	public void setUserEvaluation(int userEvaluation) {
		this.userEvaluation = userEvaluation;
	}

	public int getUserEvaluation() {
		return userEvaluation;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public AnnounceModel getAm() {
		return am;
	}

	public void setAm(AnnounceModel am) {
		this.am = am;
	}

	public void setAnnounceService(IAnnounceService announceService) {
		this.announceService = announceService;
	}

	public void setShortMessageService(IShortMessageService shortMessageService) {
		this.shortMessageService = shortMessageService;
	}

	public void setInviteService(IInviteService inviteService) {
		this.inviteService = inviteService;
	}

	public void setCanBuyService(ICanBuyService canBuyService) {
		this.canBuyService = canBuyService;
	}

	public void setTransInfoService(ITransInfoService transInfoService) {
		this.transInfoService = transInfoService;
	}

	public List<BillModel> getBillList() {
		return billList;
	}

	public void setBillList(List<BillModel> billList) {
		this.billList = billList;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public List<InviteModel> getInviteList() {
		return inviteList;
	}

	public void setInviteList(List<InviteModel> inviteList) {
		this.inviteList = inviteList;
	}

	public UserModel getUserModel() {
		return userModel;
	}

	public void setUserModel(UserModel userModel) {
		this.userModel = userModel;
	}

	public List<CanBuyModel> getCanBuyList() {
		return canBuyList;
	}

	public void setCanBuyList(List<CanBuyModel> canBuyList) {
		this.canBuyList = canBuyList;
	}

	public List<TransInfoModel> getOrders() {
		return orders;
	}

	public void setOrders(List<TransInfoModel> orders) {
		this.orders = orders;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getReceiver() {
		return receiver;
	}

	public void setReceiver(int receiver) {
		this.receiver = receiver;
	}

	public int getReplyId() {
		return replyId;
	}

	public void setReplyId(int replyId) {
		this.replyId = replyId;
	}

	public int getSmid() {
		return smid;
	}

	public void setSmid(int smid) {
		this.smid = smid;
	}

	/**
	 * 初始化我是经纪人页面(查询收到的消息）
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String initAgentInfo() {
		if (logger.isDebugEnabled()) {
			logger.debug("initAgentInfo");
		}
		if (!isLogin()) {
			session.put(Constants.URL, "initagent.htm?read=" + getRead());
			return LOGIN;
		}
		if (Constants.USER_TYPE_BROKER != getUserLogin().getUserType()) {
			session.put(Constants.URL, "initagent.htm?read=" + getRead());
			return CERTIFICATION;
		}
		ShortMessage queryMessage = new ShortMessage();
		queryMessage.setRead(read);
		if (read == 0) {
			queryMessage.setReplyTag(1);
		}
		queryMessage.setReceiver(getUserLogin().getUserid());
		queryMessage.setStartIndex((getSuperPage() - 1) * getPageSize());
		queryMessage.setPageSize(getPageSize());

		try {
			messageList = shortMessageService
					.queryShortMessagesByBroker(queryMessage);
			setTotalSize(shortMessageService
					.getShortMessageCountByBroker(queryMessage));
		} catch (Exception e) {
			logger.error("query messages error.", e);
			return ERROR;
		}

		return SUCCESS;
	}

	/**
	 * 获取发布的代购数
	 * 
	 * @return
	 */
	public String countCanBuys() {
		if (isLogin()) {
			UserModel user = getUserLogin();
			CanBuyModel statCanBuy = new CanBuyModel();
			statCanBuy.getUser().setUserid(user.getUserid());
			try {
				canBuyNum = canBuyService.getCanBuyCount(statCanBuy);
			} catch (Exception e) {
				logger.error("getCanBuyCount error.", e);
			}
		}
		return SUCCESS;
	}

	/**
	 * 获取发起的邀约数
	 * 
	 * @return
	 */
	public String countInvites() {
		if (isLogin()) {
			UserModel user = getUserLogin();
			InviteModel statInvite = new InviteModel();
			statInvite.setCanUserId(user.getUserid());
			try {
				inviteNum = inviteService.getInviteCount(statInvite);
			} catch (Exception e) {
				logger.error("getInviteNum error.", e);
			}
		}
		return SUCCESS;
	}

	/**
	 * 获取未读短消息数
	 * 
	 * @return
	 */
	public String countMessages() {
		if (isLogin()) {
			ShortMessage statMessage = new ShortMessage();
			statMessage.setReceiver(getUserLogin().getUserid());
			statMessage.setRead(0);
			statMessage.setReplyTag(1);
			try {
				messageNum = shortMessageService
						.getShortMessageCountByBroker(statMessage);
			} catch (Exception e) {
				logger.error("getMessageNum error.", e);
			}
		}
		return SUCCESS;
	}

	public String replyMsg() {
		if (!isLogin()) {
			return LOGIN;
		}
		ShortMessage replyMessage = new ShortMessage();
		replyMessage.setReceiver(receiver);
		replyMessage.setReplyTag(2);
		replyMessage.setReplyid(replyId);
		replyMessage.setMainSmid(smid);
		replyMessage.setContent(content);
		replyMessage.setCreatetime(new Date());
		replyMessage.setSender(getUserLogin().getUserid());
		try {
			shortMessageService.addReplyMessage(replyMessage);
		} catch (Exception e) {
			LOG.error("reply error.", e);
			return ERROR;
		}
		action = "initagent.htm";
		return SUCCESS;
	}

	/**
	 * 分组统计交易数量
	 * 
	 * @return
	 */
	public String groupTransInfo() {
		if (isLogin()) {
			UserModel user = getUserLogin();
			try {
				Map<Integer, Integer> groups = transInfoService
						.groupTransInfoByBroker(user.getUserid());
				deliveryNum = groups.get(1) == null ? 0 : groups.get(1);
				evaluateNum = groups.get(3) == null ? 0 : groups.get(3);
			} catch (Exception e) {
				logger.error("groupTransInfo error", e);
			}
		}
		return SUCCESS;
	}

	/**
	 * 获取公告
	 * 
	 * @return
	 */
	public String queryAnnounce() {
		try {
			am = announceService.queryLastContentByType(new AnnounceModel());
		} catch (Exception e) {
			logger.error("queryAnnounce error", e);
		}
		return SUCCESS;
	}

	/**
	 * 我发布的代购信息
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String queryShopinfo() {
		if (logger.isDebugEnabled()) {
			logger.debug("queryShopinfo");
		}
		if (!isLogin()) {
			session.put(Constants.URL, "initShopinfo.htm");
			return LOGIN;
		}
		if (Constants.USER_TYPE_BROKER != getUserLogin().getUserType()) {
			session.put(Constants.URL, "initShopinfo.htm");
			return CERTIFICATION;
		}
		userModel = super.getUserLogin();
		CanBuyModel queryModel = new CanBuyModel();
		queryModel.setStartIndex((getSuperPage() - 1) * getPageSize());
		queryModel.setPageSize(getPageSize());
		queryModel.getUser().setUserid(userModel.getUserid());
		try {
			canBuyList = canBuyService.queryCanBuys(queryModel);
			setTotalSize(canBuyService.getCanBuyCount(queryModel));
		} catch (Exception e) {
			logger.error("queryShopinfo error", e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 我发布的代购邀约
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String queryInviteinfo() {
		if (logger.isDebugEnabled()) {
			logger.debug("queryInviteinfo");
		}
		if (!isLogin()) {
			session.put(Constants.URL, "initInviteinfo.htm");
			return LOGIN;
		}
		if (Constants.USER_TYPE_BROKER != getUserLogin().getUserType()) {
			session.put(Constants.URL, "initInviteinfo.htm");
			return CERTIFICATION;
		}
		userModel = super.getUserLogin();
		InviteModel inviteModel = new InviteModel();
		inviteModel.setCanUserId(userModel.getUserid());
		inviteModel.setStartIndex((getSuperPage() - 1) * getPageSize());
		inviteModel.setPageSize(getPageSize());
		try {
			inviteList = inviteService.queryInviteInfos(inviteModel);
			setTotalSize(inviteService.getInviteCount(inviteModel));
		} catch (Exception e) {
			logger.error("queryInviteinfo error", e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 正在进行的交易
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String queryTradingInfo() {
		if (logger.isDebugEnabled()) {
			logger.debug("queryTradingInfo");
		}
		if (!isLogin()) {
			session.put(Constants.URL, "initTradingInfo.htm?state=" + state);
			return LOGIN;
		}
		if (Constants.USER_TYPE_BROKER != getUserLogin().getUserType()) {
			session.put(Constants.URL, "initTradingInfo.htm?state=" + state);
			return CERTIFICATION;
		}
		TransInfoModel queryModel = new TransInfoModel();
		queryModel.setStartIndex((getSuperPage() - 1) * getPageSize());
		queryModel.setState(state);
		queryModel.setPageSize(getPageSize());
		queryModel.getBroker().setUserid(getUserLogin().getUserid());
		queryModel.setNotEqualState(3);
		try {
			orders = transInfoService.queryTransInfos(queryModel);
			setTotalSize(transInfoService.getTransCount(queryModel));
		} catch (Exception e) {
			logger.error("queryTransInfos", e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 已完成的交易
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String queryTradedInfo() {
		if (logger.isDebugEnabled()) {
			logger.debug("queryTradingInfo");
		}
		if (!isLogin()) {
			session.put(Constants.URL, "initTradedInfo.htm?userEvaluatation="
					+ userEvaluation);
			return LOGIN;
		}
		if (Constants.USER_TYPE_BROKER != getUserLogin().getUserType()) {
			session.put(Constants.URL, "initTradedInfo.htm?userEvaluatation="
					+ userEvaluation);
			return CERTIFICATION;
		}
		TransInfoModel queryModel = new TransInfoModel();
		queryModel.setStartIndex((getSuperPage() - 1) * getPageSize());
		queryModel.setState(3);
		queryModel.setUserEvaluation(userEvaluation);
		queryModel.setPageSize(getPageSize());
		queryModel.getBroker().setUserid(getUserLogin().getUserid());
		try {
			orders = transInfoService.queryTransInfos(queryModel);
			setTotalSize(transInfoService.getTransCount(queryModel));
		} catch (Exception e) {
			logger.error("queryTransInfos", e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 查询账单
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String queryBill() {
		if (!isLogin()) {
			session.put(Constants.URL, "initBill.htm");
			return LOGIN;
		}
		if (Constants.USER_TYPE_BROKER != getUserLogin().getUserType()) {
			session.put(Constants.URL, "initBill.htm");
			return CERTIFICATION;
		}
		try {
			BillModel bill = new BillModel();
			bill.setStartIndex((getSuperPage() - 1) * getPageSize());
			bill.setBrokerId(getUserLogin().getUserid());
			billList = transInfoService.queryBill(bill);
			setTotalSize(transInfoService.getBillCount(getUserLogin()
					.getUserid()));
		} catch (Exception e) {
			logger.error("queryBill error", e);
			return ERROR;
		}
		return SUCCESS;

	}

	/**
	 * 查询账单流水
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String queryBillDetils() {
		if (!isLogin()) {
			session.put(Constants.URL, "queryBillDetils.htm");
			return LOGIN;
		}
		if (Constants.USER_TYPE_BROKER != getUserLogin().getUserType()) {
			session.put(Constants.URL, "queryBillDetils.htm");
			return CERTIFICATION;
		}
		try {
			BillModel bill = new BillModel();
			bill.setStartIndex((getSuperPage() - 1) * getPageSize());
			bill.setBrokerId(getUserLogin().getUserid());
			billList = transInfoService.queryBillDetail(bill);
			setTotalSize(transInfoService.getBillDetailCount(getUserLogin()
					.getUserid()));
		} catch (Exception e) {
			logger.error("queryBillDetails error.", e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 发布代购信息操作
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String doShopinfoOperator() {
		if (!isLogin()) {
			session.put(Constants.URL, "doShopinfoOperator.htm?status="
					+ status + "&id=" + id);
		}
		if (Constants.USER_TYPE_BROKER != getUserLogin().getUserType()) {
			session.put(Constants.URL, "doShopinfoOperator.htm?status="
					+ status + "&id=" + id);
			return CERTIFICATION;
		}
		CanBuyModel canBuyModel = new CanBuyModel();
		canBuyModel.setStatus(status);
		canBuyModel.setCanId(id);
		try {
			canBuyService.updateCanBuyStatus(canBuyModel);
		} catch (Exception e) {
			logger.error("updateCanBuyStatus Exception", e);
		}

		return SUCCESS;
	}

	public String beAgent() {
		if (logger.isDebugEnabled()) {
			logger.debug("beAgent");
		}
		if (!isLogin()) {
			return LOGIN;
		}
		userModel = super.getUserLogin();

		return SUCCESS;
	}

	public String delMessages() {
		if (logger.isDebugEnabled()) {
			logger.debug("delMessages start.");
		}
		try {
			shortMessageService.delMessages(2, smids);
		} catch (Exception e) {
			logger.error("delMessages error.", e);
			return ERROR;
		}
		if (logger.isDebugEnabled()) {
			logger.debug("delMessages end.");
		}
		action = "initagent.htm";

		return SUCCESS;
	}

	/**
	 * 已读
	 * 
	 * @return
	 */
	public String alreadyRead() {
		try {
			shortMessageService.alreadyRead(smid);
		} catch (Exception e) {
			logger.error("alreadyRead error.", e);
		}
		return null;
	}
}
